package cn.smile.controller.admin;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smile.bean.CodeMsg;
import cn.smile.bean.Result;
import cn.smile.entity.admin.Menu;
import cn.smile.service.admin.MenuService;
import cn.smile.service.admin.OperaterLogService;
import cn.smile.util.MenuUtil;
import cn.smile.util.ValidateEntityUtil;

import javax.annotation.Resource;

/**
 * 后台菜单管理控制器
 */
@RequestMapping("/admin/menu")
@Controller
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private OperaterLogService operaterLogService;

    /**
     * 菜单列表展示页面
     */
    @RequestMapping(value = "/list")
    public String list(Model model) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("title", "菜单列表");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        model.addAttribute("thirdMenus", MenuUtil.getThirdMenus(findAll));
        return "admin/menu/list";
    }

    /**
     * 菜单添加页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("title", "菜单列表");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        return "admin/menu/add";
    }

    /**
     * 菜单添加提交表单处理
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Menu menu) {
        if (Objects.isNull(menu)) {
            return Result.error(CodeMsg.DATA_ERROR);
        }
        //用统一验证实体方法验证是否合法
        Result<Boolean> checkResult = getBooleanResult(menu);
        if (!Objects.isNull(checkResult)) {
            return checkResult;
        }
        //表示验证都通过，开始添加数据库
        if (menuService.save(menu) == null) {
            Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("添加菜单信息【" + menu + "】");
        return Result.success(true);
    }

    private Result<Boolean> getBooleanResult(Menu menu) {
        CodeMsg validate = ValidateEntityUtil.validate(menu);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if (menu.getParent() != null) {
            if (menu.getParent().getId() == null) {
                menu.setParent(null);
            }
        }
        return null;
    }

    /**
     * 菜单编辑页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, @RequestParam(name = "id") Long id) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("title", "菜单列表");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        model.addAttribute("menu", menuService.find(id));
        return "admin/menu/edit";
    }

    /**
     * 菜单编辑页面表单提交处理
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Menu menu) {
        if (Objects.isNull(menu)) {
            return Result.error(CodeMsg.DATA_ERROR);
        }
        if (menu.getId() == null) {
            return Result.error(CodeMsg.ADMIN_MENU_ID_EMPTY);
        }
        //用统一验证实体方法验证是否合法
        Result<Boolean> validate = getBooleanResult(menu);
        if (validate != null) return validate;
        Menu existMenu = menuService.find(menu.getId());
        if (existMenu == null) {
            return Result.error(CodeMsg.ADMIN_MENU_ID_ERROR);
        }
        //表示验证都通过，开始添加数据库
        existMenu.setIcon(menu.getIcon());
        existMenu.setName(menu.getName());
        existMenu.setParent(menu.getParent());
        existMenu.setSort(menu.getSort());
        existMenu.setUrl(menu.getUrl());
        existMenu.setButton(menu.isButton());
        existMenu.setShow(menu.isShow());
        if (menuService.save(existMenu) == null) {
            return Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("编辑菜单信息【" + existMenu + "】");
        return Result.success(true);
    }

    /**
     * 删除菜单信息
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id") Long id) {
        try {
            menuService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("删除菜单信息，菜单ID【" + id + "】");
        return Result.success(true);
    }
}
